//
//  SuperviseArrageDetailView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/1/10.
//

import Foundation
import UIKit

class RwTzItemParam {
    var rwxid: Int64? // 任务项ID
    var rwjlid: Int64? // 任务ID
    var zgzt: String?
    var tzsm: String?
    var fjImgs: [UIImage]?
    
    init(rwxid: Int64? = nil, rwjlid: Int64? = nil, zgzt: String? = nil, tzsm: String? = nil, fjImgs: [UIImage]? = nil) {
        self.rwxid = rwxid
        self.rwjlid = rwjlid
        self.zgzt = zgzt
        self.tzsm = tzsm
        self.fjImgs = fjImgs
    }
}

class RwjlTzParam: Encodable {
    var rwxid: Int64? // 任务项ID
    var rwjlid: Int64? // 任务ID
    var zgzt: String?
    var tzsm: String?
    var zgfjs: [FjdzParam]?
        
    init(item: RwTzItemParam, images: [FjdzParam]? = nil) {
        self.rwxid = item.rwxid
        self.rwjlid = item.rwjlid
        self.zgzt = item.zgzt
        self.tzsm = item.tzsm
        self.zgfjs = images
    }
}

class RwZgTzParam: Encodable {
    var id: Int64 // 任务ID
    var zgjls: [RwjlTzParam]?
    
    init(id: Int64, zgjls: [RwjlTzParam]? = nil) {
        self.id = id
        self.zgjls = zgjls
    }
}

class SuperviseArrageDetailView: UIView {
    let submitBtn = UIButton.createPrimaryLarge("提交")
    let contentV = UIScrollView()
    let checkTF = ErrorCheckEffect()
    
    var zgjls : [RwTzItemParam] = []

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModals modals: [SpRwbgtzModal?]) {
        contentV.removeAllSubViews()
        
        var lastI: ErrorCheckEffect?
        for (index, m) in modals.enumerated() {
            let role = UserDefaults.standard.string(forKey: SafetyUserRole)
            let itemV = ErrorCheckEffect(withEnable: ((m?.sfzg) == "1") && role == UserRole.Lattice.rawValue)
            itemV.checkR.isCheck = m?.rwzt == "2" ? true : nil
            itemV.title = m?.rwmc ?? ""
            itemV.didSelectCheckWith = { (isCheck, msg, imgs) -> () in
                var zgzt: String? = nil
                if isCheck == true {
                    zgzt = "1"
                } else if isCheck == false {
                    zgzt = "0"
                }
                
                var index = -1
                for (i, zg) in self.zgjls.enumerated() {
                    if zg.rwxid == m?.id {
                        index = i
                    }
                }
                
                if index > -1 {
                    self.zgjls.remove(at: index)
                }
                
                if zgzt != nil {
                    self.zgjls.append(RwTzItemParam(rwxid: m?.id, rwjlid: m?.rwjlid, zgzt: zgzt, tzsm: msg, fjImgs: imgs))
                }
                
            }
            contentV.addSubview(itemV)
            itemV.snp.makeConstraints { make in
                make.centerX.equalToSuperview()
                make.width.equalTo(ScreenWidth)
                make.top.equalTo(lastI == nil ? contentV.snp.top : lastI!.snp.bottom).offset(10)
                if index == modals.count - 1 {
                    make.bottom.equalToSuperview().offset(-20)
                }
            }
            lastI = itemV
        }
    }
    
    func setupUI() {
        let role = UserDefaults.standard.string(forKey: SafetyUserRole)
        let isHide = role != UserRole.Lattice.rawValue
        if !isHide {
            addSubview(submitBtn)
            submitBtn.snp.makeConstraints { (make) -> Void in
                make.bottom.equalToSuperview().offset(-14)
                make.height.equalTo(50)
                make.centerX.equalTo(self.snp.centerX)
                make.width.equalTo(ScreenWidth - 20)
            }
        }
        
        contentV.keyboardDismissMode = .interactive
        contentV.showsVerticalScrollIndicator = false
        addSubview(contentV)
        contentV.snp.makeConstraints { (make) -> Void in
            make.top.equalToSuperview()
            make.centerX.equalTo(self.snp.centerX)
            make.width.equalTo(ScreenWidth)
            if isHide {
                make.bottom.equalTo(self.snp.bottom)
            } else {
                make.bottom.equalTo(submitBtn.snp.top).offset(-26)
            }
        }
        
        checkTF.imgPicker.rowCount = 4
        checkTF.imgPicker.isRequired = true
        checkTF.imgPicker.key = "现场图像"
        contentV.addSubview(checkTF)
        checkTF.snp.makeConstraints { (make) -> Void in
            make.centerX.equalToSuperview()
            make.width.equalTo(ScreenWidth - 20)
            make.top.equalTo(contentV.snp.top).offset(10)
            make.bottom.equalToSuperview()
        }
    }
}
